package com.xiaohudie.SGGJUC;

import java.util.concurrent.*;

/**
 * @Author xiao hu die
 * @Date 2022/3/8 12:22
 * @Version 1.0
 */
public class TestCallable {
    public static void main(String[] args) {
        /**只有FutureTask能接收callable*/
        FutureTask<String> stringFutureTask = new FutureTask<>(new TestFutureTask());
        CopyOnWriteArrayList<String> strings = new CopyOnWriteArrayList<>();
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i <10 ; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String s = stringFutureTask.get();
                        System.out.println(s);
                        strings.add(s);
                        countDownLatch.countDown();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            },String.valueOf(i)).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(strings);
    }
}
class TestFutureTask implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "陈唯怡";
    }
}